﻿using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Flavor;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using VsCommands = Microsoft.VisualStudio.VSConstants.VSStd97CmdID;
using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
using Microsoft.VisualStudio.OLE.Interop;

namespace VSSDKAssist.Platform.FlavorProjects
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("70E797E8-3704-4EEC-A0F3-9922265C258E")]
    public class VsPackageSetupProject : FlavoredProjectBase
    {
        #region Fields
        private VsPackage package;
        private static Icon projectIcon;
        private static Guid guid1;
        #endregion

        #region Constructors
        static VsPackageSetupProject()
        {
            projectIcon = new Icon(typeof(VsPackageSetupProject).Assembly.GetManifestResourceStream("VSSDKAssist.Platform.Resources.VsPackageSetupProject.ico"));
            guid1 = new Guid("79989DD6-4C13-4D10-9872-73538668D037");
        }

        public VsPackageSetupProject(VsPackage package)
        {
            this.package = package;
        }
        #endregion

        protected override void SetInnerProject(IntPtr innerIUnknown)
        {
            object inner = null;

            inner = Marshal.GetObjectForIUnknown(innerIUnknown);

            if(base.serviceProvider == null)
            {
                base.serviceProvider = this.package;
            }

            base.SetInnerProject(innerIUnknown);
        }

        protected override void InitializeForOuter(string fileName, string location, string name, uint flags, ref Guid guidProject, out bool cancel)
        {
            base.InitializeForOuter(fileName, location, name, flags, ref guidProject, out cancel);
        }

        protected override int GetProperty(uint itemId, int propId, out object property)
        {
            switch(propId)
            {
                case (int)__VSHPROPID.VSHPROPID_IconIndex:
                case (int)__VSHPROPID.VSHPROPID_OpenFolderIconIndex:
                    {
                        if(itemId == VSConstants.VSITEMID_ROOT)
                        {
                            property = null;
                            //Forward to __VSHPROPID.VSHPROPID_IconHandle and __VSHPROPID.VSHPROPID_OpenFolderIconHandle propIds
                            return VSConstants.E_NOTIMPL;
                        }

                        break;
                    }
                case (int)__VSHPROPID.VSHPROPID_IconHandle:
                case (int)__VSHPROPID.VSHPROPID_OpenFolderIconHandle:
                    {
                        if(itemId == VSConstants.VSITEMID_ROOT && projectIcon != null)
                        {
                            property = projectIcon.Handle;
                            return VSConstants.S_OK;
                        }

                        break;
                    }
            }

            return base.GetProperty(itemId, propId, out property);
        }

        protected override int ExecCommand(uint itemid, ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if(pguidCmdGroup == VsMenus.guidStandardCommandSet97)
            {
                switch((VsCommands)nCmdID)
                {
                    case VsCommands.AddClass:
                    case VsCommands.SetStartupProject:
                        return VSConstants.S_OK;
                }
            }
            else if(pguidCmdGroup == VsMenus.guidStandardCommandSet2K)
            {
                switch(nCmdID)
                {
                    case 1648: //Run Code Analysis
                    case 1130: //Add Service Reference
                    case (uint)VsCommands2K.ADDWFCFORM:
                    case (uint)VsCommands2K.ADDUSERCONTROL:
                    case (uint)VsCommands2K.ADDTBXCOMPONENT:
                    case (uint)VsCommands2K.PROJSTARTDEBUG:
                    case (uint)VsCommands2K.PROJSTEPINTO:
                    case (uint)VsCommands2K.ViewInClassDiagram:
                        return VSConstants.S_OK;
                }
            }
            else if(pguidCmdGroup == guid1)
            {
                switch(nCmdID)
                {
                    case 768: //Generate Code Metrics
                        return VSConstants.S_OK;
                }
            }

            return base.ExecCommand(itemid, ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
        }

        protected override int QueryStatusCommand(uint itemid, ref Guid pguidCmdGroup, uint cCmds, Microsoft.VisualStudio.OLE.Interop.OLECMD[] prgCmds, IntPtr pCmdText)
        {
            if(pguidCmdGroup == VsMenus.guidStandardCommandSet97)
            {
                switch((VsCommands)prgCmds[0].cmdID)
                {
                    case VsCommands.AddClass:
                    case VsCommands.SetStartupProject:
                        prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_INVISIBLE | OLECMDF.OLECMDF_SUPPORTED);
                        return VSConstants.S_OK;
                }
            }
            else if(pguidCmdGroup == VsMenus.guidStandardCommandSet2K)
            {
                switch(prgCmds[0].cmdID)
                {
                    case 1648: //Run Code Analysis
                    case 1130: //Add Service Reference
                    case (uint)VsCommands2K.ADDWFCFORM:
                    case (uint)VsCommands2K.ADDUSERCONTROL:
                    case (uint)VsCommands2K.ADDTBXCOMPONENT:
                    case (uint)VsCommands2K.PROJSTARTDEBUG:
                    case (uint)VsCommands2K.PROJSTEPINTO:
                    case (uint)VsCommands2K.ViewInClassDiagram:
                        prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_INVISIBLE | OLECMDF.OLECMDF_SUPPORTED);
                        return VSConstants.S_OK;
                }
            }
            else if(pguidCmdGroup == guid1)
            {
                switch(prgCmds[0].cmdID)
                {
                    case 768: //Generate Code Metrics
                        prgCmds[0].cmdf = (uint)(OLECMDF.OLECMDF_INVISIBLE | OLECMDF.OLECMDF_SUPPORTED);
                        return VSConstants.S_OK;
                }
            }

            return base.QueryStatusCommand(itemid, ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
        }
    }
}